package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.integral;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.MemberInfoResultBean;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ViewBgUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 积分扣除
 */
public class IntegralFragment extends BaseFragment {
    public static IntegralFragment newInstance(String uuid) {
        IntegralFragment integralFragment = new IntegralFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        integralFragment.setArguments(bundle);
        return integralFragment;
    }

    @BindView(R.id.tv_kouchu)
    TextView tvKouChu;
    @BindView(R.id.et_integral_kc)
    EditText etIntegralKC;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_integral_phone)
    TextView tvPhone;
    @BindView(R.id.tv_zong_integral)
    TextView tvZongIntegral;
    private IntegralMode integralMode;
    private MemberInfoResultBean memberInfoResultBean;

    @Override
    protected int initLayout() {
        return R.layout.fg_integral;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.PointsDeduction), getActivity());
        etIntegralKC.setBackground(ViewBgUtil.setWidthBg(15,1, Color.parseColor("#00000000"),Color.parseColor("#D7D7D7")));
    }

    @Override
    protected void initData(Context mContext) {
        integralMode = getViewModelProvider().get(IntegralMode.class);
    }

    @Override
    protected void loadMethod() {
        getUUIDMemeberData();
        integralKC();
    }

    @OnClick({R.id.tv_kouchu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_kouchu:
                tvKouChu.setClickable(false);
                tvKouChu.setFocusable(false);
                String integerInput = etIntegralKC.getText().toString().trim();
                if (StringUtils.isEmpty(integerInput)) {
                    ToastUtils.showToast(getContext().getApplicationContext(), "请输入扣除积分！");
                    tvKouChu.setClickable(true);
                    tvKouChu.setFocusable(true);
                    return;
                }
                if ("0".equals(integerInput)) {
                    ToastUtils.showToast(getContext().getApplicationContext(), "请输入正确的积分！");
                    tvKouChu.setClickable(true);
                    tvKouChu.setFocusable(true);
                    return;
                }
                int input = ConverterHelper.toBigDecimal(integerInput).intValue();
                if (input > memberInfoResultBean.getNewIntegral()) {
                    ToastUtils.showToast(getContext().getApplicationContext(), "您输入的积分过大！");
                    tvKouChu.setClickable(true);
                    tvKouChu.setFocusable(true);
                    return;
                }
                String intIntegral = ConverterHelper.toBigDecimal(integerInput).multiply(ConverterHelper.toBigDecimal(1)).toString();
                integralMode.IntegralPay(getActivity(), memberInfoResultBean.getPhones(), intIntegral);
                break;

        }
    }

    /**
     * 根据UUID查询会员信息
     */
    private void getUUIDMemeberData() {
        integralMode.getLiveData().observe(this, memberInfoResultBean -> {
            tvPhone.setText(memberInfoResultBean.getPhone());
            tvName.setText(memberInfoResultBean.getRealName());
            tvZongIntegral.setText(memberInfoResultBean.getNewIntegral()+"");
            this.memberInfoResultBean = memberInfoResultBean;
        });
        integralMode.MemberByUuid(getActivity(), getArguments().getString("uuid"));
    }

    /**
     * 积分扣除
     */
    private void integralKC() {
        integralMode.getLiveCodeData().observe(this, integer -> {
            if (integer == 200) {
                startWithPop(IntegralSufFragment.newInstance());
            }
        });
    }
}
